﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BAL
{
    public partial class LockStoreBill : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        //Socket cliSocket = null;
        string ipAddr = "192.168.1.211";
        int port = 1024;
        private string ID;
        
       

        public LockStoreBill()
        {
            InitializeComponent();
        }

        public LockStoreBill(string _ID)
        {
            InitializeComponent();
            this.ID = _ID;
        }

        private void LockStoreBill_Load(object sender, EventArgs e)
        {
            tb_FBrNo.Visible = false;
            DataGridStyle();
            BinData();
        }

        //获取数据
        private void BinData()
        {
            DataSet ds = bjs.GetLockStoreBill();
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            dataGrid1.DataSource = dt;
            this.tb_PkgsBarCode.Text = "";
            this.tb_FBrNo.Text = "";
            this.tb_PlaceBarCode.Text = "";
            this.tb_HelperCode.Text = "";
            this.tb_PlaceName.Text = "";
            this.tb_WorkBillNo.Text = "";
        }

        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "LockStoreBill";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "流水号";
            cl1.MappingName = "FBrNo";
            cl1.Width = 0;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl7 = new DataGridTextBoxColumn();
            cl7.HeaderText = "标记";
            cl7.MappingName = "stype";
            cl7.Width = 25;
            dts.GridColumnStyles.Add(cl7);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "助记码";
            cl2.MappingName = "HelperCode";
            cl2.Width = 45;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "包装码";
            cl3.MappingName = "PkgsBarCode";
            cl3.Width = 80;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
            cl6.HeaderText = "生产任务单";
            cl6.MappingName = "WorkBillNo";
            cl6.Width = 110;
            dts.GridColumnStyles.Add(cl6);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "仓位码";
            cl4.MappingName = "PlaceBarCode";
            cl4.Width = 80;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
            cl5.HeaderText = "仓位名";
            cl5.MappingName = "PlaceName";
            cl5.Width = 80;
            dts.GridColumnStyles.Add(cl5);

          
            dataGrid1.TableStyles.Add(dts);

        }

        private void tb_PkgsBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            bool lb = false;
            string FBrNo = "";
            string PlaceBarCode = "";
            string PlaceName = "";
            string HelperCode = "";
            string WorkBillNo="";

            if (e.KeyCode == Keys.Enter)
            {
                string PkgsBarCode = this.tb_PkgsBarCode.Text;
                if (PkgsBarCode == "" || PkgsBarCode == null)
                {
                    MessageBox.Show("请扫描包装码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else
                {
                    lb = bjs.GetLockStore(PkgsBarCode, out FBrNo, out HelperCode, out PlaceBarCode, out PlaceName, out  WorkBillNo);
                    if (lb) 
                    {
                        this.tb_FBrNo.Text = FBrNo;
                        this.tb_PlaceBarCode.Text = PlaceBarCode;
                        this.tb_HelperCode.Text = HelperCode;
                        this.tb_PlaceName.Text = PlaceName;
                        this.tb_WorkBillNo.Text = WorkBillNo;
                        this.bt_finish.Focus();
                    }
                    else
                    {
                        MessageBox.Show("扫描包装码没有未审核拆包单！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        return;
                    }
                }
            }
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            string FBrNo = ((DataTable)(dataGrid1.DataSource)).Rows[index]["FBrNo"].ToString();
            string PkgsBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index]["PkgsBarCode"].ToString();
            string HelperCode = ((DataTable)(dataGrid1.DataSource)).Rows[index]["HelperCode"].ToString();
            string PlaceBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index]["PlaceBarCode"].ToString();
            string PlaceName = ((DataTable)(dataGrid1.DataSource)).Rows[index]["PlaceName"].ToString();
            string WorkBillNo = ((DataTable)(dataGrid1.DataSource)).Rows[index]["WorkBillNo"].ToString();

            this.tb_FBrNo.Text = FBrNo.Trim();
            this.tb_HelperCode.Text = HelperCode.Trim();
            this.tb_PkgsBarCode.Text = PkgsBarCode;
            this.tb_PlaceBarCode.Text = PlaceBarCode;
            this.tb_PlaceName.Text = PlaceName;
            this.tb_WorkBillNo.Text = WorkBillNo;
        }

        private void bt_refresh_Click(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();
        }

        private void bt_finish_Click(object sender, EventArgs e)
        {
            bool lb = false;
            string FBrNo = this.tb_FBrNo.Text.Trim();
            string WorkBillNo = this.tb_WorkBillNo.Text.Trim();
            string PkgsBarCode = this.tb_PkgsBarCode.Text.Trim();

            if (FBrNo == "" || FBrNo == null)
            {
                MessageBox.Show("请扫描包装码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            if (PkgsBarCode == "" || PkgsBarCode == null)
            {
                MessageBox.Show("请扫描包装码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            else
            {
                lb = bjs.ExamineLockStore(FBrNo, PkgsBarCode, WorkBillNo);
                if (lb)
                {
                    DataGridStyle();
                    BinData();
                }
                else 
                {
                    MessageBox.Show("审核拆包单失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
            }
        }

        private void bt_back_Click(object sender, EventArgs e)
        {
            Main2 main = new Main2(this.ID);
            main.Show();
            this.Hide();
        }

        private void label4_ParentChanged(object sender, EventArgs e)
        {

        }

        private void tb_HelperCode_TextChanged(object sender, EventArgs e)
        {

        }

        
    }
}